package com.epfresh.supplier.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.MoneyPathEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderMinusMoneyInfoActivity extends BaseActivity {

    static final String TAG = "OrderMinusMoneyInfoActivity";
    String id;

    TextView tvName,tvNumber,tvMoney;
    ListView lv;
    ArrayList<MoneyPathEntity.ListBean> list = new ArrayList<>();
    MoneyAdapter moneyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarTitle("钱款去向");
        initView();
        id = getIntent().getStringExtra("id");
        Log.i("id", id);
        if(id!=null) {
            mHttpInfo();
        }else{
            T.toast("数据有误，请重试");
            finish();
        }
    }
    private void initView() {
        setContentView(R.layout.activity_order_minus_moneyinfo);
        tvName = (TextView)findViewById(R.id.tv_name);
        tvNumber = (TextView)findViewById(R.id.tv_number);
        tvMoney = (TextView)findViewById(R.id.tv_money);
        lv = (ListView)findViewById(R.id.lv);
        moneyAdapter = new MoneyAdapter(this,list,R.layout.item_money_and_logistics);
        lv.setAdapter(moneyAdapter);
        lv.setDivider(null);
        ivBack.setVisibility(View.VISIBLE);
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            default:
                break;
        }
    }
	class MoneyAdapter extends CommonAdapter<MoneyPathEntity.ListBean> {
		public MoneyAdapter(Context context, List<MoneyPathEntity.ListBean> historyList, int a) {
			super(context, historyList, a);
		}

		@Override
		public void convert(ViewHolder holder, final MoneyPathEntity.ListBean bean) {
			holder.setText(R.id.tv_content, bean.getTitle());
            holder.setText(R.id.tv_time, bean.getNodeTime());
            if(list.indexOf(bean)==list.size()-1){
                holder.getView(R.id.view_2).setVisibility(View.INVISIBLE);
            }
            if(list.indexOf(bean)==0){
                holder.getView(R.id.view_1).setVisibility(View.INVISIBLE);
                ((ImageView)holder.getView(R.id.iv_state)).setImageResource(R.mipmap.order_red_dot);
            }
		}
	}

    private void mHttpInfo() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_jmt_refundTrack);
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_jmt_refundTrack, new OnRequestListener<MoneyPathEntity>() {
            @Override
            public MoneyPathEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, MoneyPathEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                showExceptionView(R.mipmap.upload_error, "网路错误", HttpRequest.FAIL_NET_FAULT);
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                showExceptionView(R.mipmap.upload_error, arg + "", HttpRequest.FAIL_NET_FAULT);
            }

            @Override
            public void onStart(Object tag) {
                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<MoneyPathEntity> response, Object tag) {
                hideProgressDialog();
                MoneyPathEntity responseElement = response.getResponseElement();
                updateOrderInfo(responseElement);
            }

        });
    }
    void updateOrderInfo(MoneyPathEntity moneyPath){
        tvName.setText(moneyPath.getPayWay());
        tvNumber.setText(moneyPath.getReceiveAccount());
        tvMoney.setText(moneyPath.getMoney()+"元");
        list.addAll(moneyPath.getList());
        moneyAdapter.notifyDataSetChanged();
    }

}
